package com.produce.user.controller;


import com.common.dao.base.constant.SystemStaticConst;
import com.common.dao.base.controller.BaseController;
import com.common.dao.base.service.BaseService;
import com.produce.user.entity.User;
import com.produce.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 类描述：用户的controller交互层
 * @author linzf
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController<User> {

    @Autowired
    private UserService userService;

    @Override
    protected BaseService<User> getService() {
        return this.userService;
    }

    /**
     * 功能描述：获取用户
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("getUser")
    public Map<String,Object> getUser(@RequestBody Map<String,Object> user){
        Map<String,Object> result = new HashMap<>(3);
        Map<String,Object> entity = userService.getUser(user);
        if(entity!=null){
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"获取用户数据成功！");
            result.put("entity",entity);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"获取用户数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：实现修改用户
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("updateUser")
    public Map<String,Object> updateUser(@RequestBody Map<String,Object> entity) throws Exception {
        boolean success = userService.updateUser(entity);
        Map<String,Object> result = new HashMap<>(2);
        if(success==true){
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"修改用户成功！");
            result.put("entity",entity);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"修改用户失败！");
        }
        return result;
    }

    /**
     * 功能描述：实现新增用户
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("saveUser")
    public Map<String,Object> saveUser(@RequestBody Map<String,Object> entity) throws Exception {
        boolean success = userService.saveUser(entity);
        Map<String,Object> result = new HashMap<>(2);
        if(success==true){
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"增加用户成功！");
            result.put("entity",entity);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"增加用户失败！");
        }
        return result;
    }


}
